package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Set;

import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.compress.utils.Sets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.BywStageMapper;
import com.ruoyi.system.domain.BywStage;
import com.ruoyi.system.service.IBywStageService;

/**
 * 达人基础Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-25
 */
@Service
public class BywStageServiceImpl implements IBywStageService 
{
    @Autowired
    private BywStageMapper bywStageMapper;

    /**
     * 查询达人基础
     * 
     * @param stageId 达人基础主键
     * @return 达人基础
     */
    @Override
    public BywStage selectBywStageByStageId(Long stageId)
    {
        return bywStageMapper.selectBywStageByStageId(stageId);
    }

    @Override
    public List<BywStage> selectBywStageByTiktokAccount(String tiktokAccount)
    {
        return bywStageMapper.selectBywStageByTiktokAccount(tiktokAccount);
    }
    /**
     * 查询达人基础列表
     * 
     * @param bywStage 达人基础
     * @return 达人基础
     */
    @Override
    public List<BywStage> selectBywStageList(BywStage bywStage)
    {
        return bywStageMapper.selectBywStageList(bywStage);
    }

    /**
     * 新增达人基础
     * 
     * @param bywStage 达人基础
     * @return 结果
     */
    @Override
    public int insertBywStage(BywStage bywStage)
    {
        bywStage.setCreateTime(DateUtils.getNowDate());
        return bywStageMapper.insertBywStage(bywStage);
    }

    /**
     * 修改达人基础
     * 
     * @param bywStage 达人基础
     * @return 结果
     */
    @Override
    public int updateBywStage(BywStage bywStage)
    {
        bywStage.setUpdateTime(DateUtils.getNowDate());
        return bywStageMapper.updateBywStage(bywStage);
    }

    /**
     * 批量删除达人基础
     * 
     * @param stageIds 需要删除的达人基础主键
     * @return 结果
     */
    @Override
    public int deleteBywStageByStageIds(Long[] stageIds)
    {
        return bywStageMapper.deleteBywStageByStageIds(stageIds);
    }

    /**
     * 删除达人基础信息
     * 
     * @param stageId 达人基础主键
     * @return 结果
     */
    @Override
    public int deleteBywStageByStageId(Long stageId)
    {
        return bywStageMapper.deleteBywStageByStageId(stageId);
    }

    @Override
    public void saveOrUpdateStages(List<BywStage> stageList) {
        List<BywStage> stageListOld = bywStageMapper.selectBywStageList(new BywStage());
        if(stageListOld.size() == 0){
            bywStageMapper.batchInsertBywStage(stageList);
        }else{
            Set<String> kesCheck = Sets.newHashSet();
            stageListOld.stream().forEach(s->{
                kesCheck.add(s.getUserId()+":" + s.getTiktokAccount()); }
            );
            List<BywStage> stageListNew = Lists.newArrayList();
            List<BywStage> stageListExist = Lists.newArrayList();
            stageList.stream().forEach(s -> {
                if(kesCheck.contains(s.getUserId()+":" + s.getTiktokAccount())){
                    stageListExist.add(s);
                }else{
                    stageListNew.add(s);
                }
            });

            if(stageListNew.size()> 0){
                bywStageMapper.batchInsertBywStage(stageListNew);
            }

            if(stageListExist.size()> 0){
                for(BywStage stage : stageListExist){
                    bywStageMapper.singleUpdateBywStage(stage);

                }
//                bywStageMapper.batchUpdateBywStage(stageListExist);
            }

        }

    }
}
